package cn.daniellee.plugin.wl;

import cn.daniellee.plugin.wl.command.LimiterCommand;
import cn.daniellee.plugin.wl.core.LimitCore;
import cn.daniellee.plugin.wl.listener.PlayerListener;
import cn.daniellee.plugin.wl.model.LimitWorld;
import cn.daniellee.plugin.wl.runnable.LimiterRunnable;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class WorldLimiter extends JavaPlugin {

	private static final String PACKAGE_VERSION = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];

	private static WorldLimiter instance;

	private String prefix;

	private boolean bungeecord;

	private BukkitTask limiterTask;

	private LimitCore limitCore = new LimitCore();

	public void onEnable(){
		instance = this;

		loadConfig();
		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> WorldLimiter Loaded <<<<<<<<<<<<<<<<<<<<<<<<");
		getLogger().info(">>>>> If you encounter any bugs, please contact author's QQ: 768318841 <<<<<");
		getLogger().info(" ");

		Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);
		Bukkit.getPluginCommand("worldlimiter").setExecutor(new LimiterCommand());

		bungeecord = Bukkit.getServer().spigot().getConfig().getBoolean("settings.bungeecord", false);

		if (bungeecord) this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");

		limiterTask = new LimiterRunnable().runTaskTimer(WorldLimiter.getInstance(), 0, 20);
	}

	public void loadConfig() {
		getLogger().info("Loading config...");
		if(!getDataFolder().exists()) getDataFolder().mkdirs();
		File config = new File(getDataFolder(), "config.yml");
		// 如果是第一次加载插件
		if (!config.exists()) {
			TimeZone timeZone = TimeZone.getDefault();
			// 如果服务器不是在亚洲时区
			if (!timeZone.getID().equals("Asia/Shanghai")) {
				// 释放英文版配置文件
			    saveResource("config-en.yml", false);
			    File configEn = new File(getDataFolder(), "config-en.yml");
			    if (configEn.exists()) {
			        if (configEn.renameTo(config)) {
                        reloadConfig();
                    }
                }
			}
		}
		prefix = "&7[&b" + getConfig().getString("prompt-prefix", "&6WorldLimiter") + "&7] &3: &r";
		limitCore.load();
		saveDefaultConfig();
	}

	@Override
	public void onDisable() {
		if (bungeecord) this.getServer().getMessenger().unregisterOutgoingPluginChannel(this, "BungeeCord");

		if (limiterTask != null) limiterTask.cancel();

		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> WorldLimiter Unloaded <<<<<<<<<<<<<<<<<<<<<<<<");
		getLogger().info(" ");
	}

	public static WorldLimiter getInstance() {
		return instance;
	}

	public void sendBroadcast(String broadcast) {
		if (bungeecord) {
			List<Player> players = (List<Player>) Bukkit.getOnlinePlayers();
			if (!players.isEmpty()) {
				ByteArrayDataOutput out = ByteStreams.newDataOutput();
				out.writeUTF("Message");
				out.writeUTF("ALL");
				out.writeUTF(broadcast);
				players.get(0).sendPluginMessage(this, "BungeeCord", out.toByteArray());
			}
		} else Bukkit.broadcastMessage(broadcast);
	}

	public String getPrefix() {
		return prefix;
	}

	public static Class<?> getNMSClass(String nmsClassName) throws ClassNotFoundException {
		return Class.forName("net.minecraft.server." + PACKAGE_VERSION + "." + nmsClassName);
	}

}